var RootUrl = 'http://leduobao.com/';//全局域名
var IosVersion = 4;//全局IOS版本
var VerChannel= 'ios';//渠道识别
/*全局AJAX方法*/
function ajaxRequest(url, method, datas, callBack) {
	//判断网络
    var connectionType = api.connectionType;
    if(connectionType=="none"){
    	api.openWin({
	        name: 'error-win',
	        url: '../error/error-win.html'
        });
    }
	var serverUrl;
    if(api.systemType != 'ios'){
         serverUrl = RootUrl + '/?/newapi';
        }else{
         serverUrl = RootUrl + '/?/newiosapi';
        }
    api.ajax({
        url: serverUrl + url,
        method: method,
        cache: true,
        timeout: 60,
        dataType: 'json',
        data: {
            values: datas
        }
    }, function (ret, err) {
        callBack(ret, err);
    });
}

//更新用户本地储存数据
/*function get_user_data(){
	if(userid){
		var get_user_url = '/ajax/app_user_info/'+userid+'/'+password;
		ajaxRequest(get_user_url, 'GET', '', function(ret, err){
			if(ret){
				$api.rmStorage('money');
				$api.rmStorage('headimg');
				$api.rmStorage('myscore');
				$api.setStorage('money',ret.money);
				$api.setStorage('myscore',ret.myscore);
				$api.setStorage('headimg',ret.headimg);
			}
		});	
	}
}*/
//更新用户本地储存数据
function get_user_data(){
	if($api.getStorage('userid')){
		var userdatas={};
		userdatas['userid']=$api.getStorage('userid');
		userdatas['password']=$api.getStorage('password');
		var get_user_url = '/ajax/app_user_info';
		ajaxRequest(get_user_url, 'POST', userdatas, function(ret, err){
			if(ret.status==1){
				$api.rmStorage('money');
				$api.rmStorage('headimg');
				$api.rmStorage('username');
				$api.rmStorage('myscore');
				$api.setStorage('money',ret.money);
				$api.setStorage('username',ret.username);
				$api.setStorage('headimg',ret.headimg);		
				$api.setStorage('myscore',ret.myscore);			
			}else{
				$api.clearStorage();
				//退出登录
				/*var setCss = 'setCss(4);';
					api.execScript({
						name: 'root',
						script: setCss
				});
				api.closeToWin({
				    name: 'root',
				    animation: {
				        type: 'flip',
				        subType: 'from_bottom',
				        duration: 500
				    }
				});*/
				api.openWin({
	                    name: 'my-body',
	                    url: 'widget://html/login/login-body.html',
                });
			}
		});	
		/*setTimeout(function(){
			api.execScript({
				name: 'root',
				frameName: 'my-body',
				script: 'upnew_user_info();'
			});
		//},1000);*/
	}
}
//Plus绑定
function BindPlus(userid,username){
	var push = api.require('push');
	push.bind({
	    userName:username,
	    userId:userid
	},function(ret,err){
	});
}
function echoInit() {
         echo.init({
                offset : 0,
                throttle : 300 //设置图片延迟加载的时间
        });
}
//打开商品详情页
function common_Open_shop(shopid){
	api.openWin({
	    name: shopid,
	    url: 'widget://html/shop/shop-win.html',
	    pageParam: {
       		shopid: shopid
    	}
    });
}
//加入购物车
function common_Add_shop(shopid){
	var addshoplist_url = '/ajax/GoShopCart/'+shopid+'/1';//添加数量1
	ajaxRequest(addshoplist_url, 'GET', '', function(ret, err){
		if(ret){
			if(ret.status==0){
				api.toast({
		            msg:'添加成功'
	            });			
			}else{
				api.toast({
		            msg:'添加失败'
	            });
			}
		}else{
			api.toast({
	            msg:'添加失败'
            });
		}
	});
}
//购买商品
function common_now_buy(shopid){
	common_Add_shop(shopid);//添加购物车
	api.execScript({
        name:'root',
	    script: 'setCss(3);'
    });
    api.closeToWin({
	    name: 'root'
    });
}
//跳转最新一期
function toshop(shopid){
	var to_shop_url = '/ajax/get_new_shop/'+shopid;
	ajaxRequest(to_shop_url, 'GET', '', function(ret, err){
		if(ret){
			common_Open_shop(ret.id);
		}
	});
}

//打开晒单详情
function common_open_shaidan(sd_id){
	api.openWin({
	    name: sd_id,
	    url: 'widget://html/shaidan/shaidan-win.html',
	    pageParam: {
       		sd_id: sd_id
    	}
    });
}
//打开用户信息页
function common_open_user(userid){
	api.openWin({
	    name: userid,
	    url: 'widget://html/user/user-win.html',
	    pageParam: {
       		userid: userid
    	}
    });
}
/*打开往期揭晓*/
function common_open_wangqi(shopid){
	api.openWin({
	    name: 'wangqi-win',
	    url: 'widget://html/wangqi/wangqi-win.html',
	    pageParam: {
       		shopid: shopid
    	}
    });
}
//打开单个IP晒单分享
function common_open_fenx(sid){
	api.openWin({
	    name: 'fenx-win',
	    url: 'widget://html/fenx/fenx-win.html',
	    pageParam: {
       		sid: sid
    	}
    });
}
//打开计算规则
function common_open_rules(){
	api.openWin({
	    name: 'rules-win',
	    url: 'widget://html/rules/rules-win.html',
    });
}
//打开计算详情
function common_open_onerules(shopid){
	api.openWin({
	    name: 'onerules-win',
	    url: 'widget://html/rules/onerules-win.html',
	    pageParam: {
       		shopid: shopid
    	}
    });
}

//打开分享
function common_share(){
	api.openFrame({
	    name: 'share-body',
	    url: 'widget://html/share/share-body.html',
	    rect: {
		    x:0,
		    y:0,
	    }
    });
}
//关闭分享
function closeShareFrame() {
	$api.css($api.byId('share-main'),'animation:swiperend 0.3s;-webkit-animation:swiperend 0.3s;height:0;');
	setTimeout(function() {
		api.closeFrame({
	       name: 'share-body'
        });
	}, 300);
}
//聊天室
function comment(){
		   var userid = $api.getStorage('userid');
		   var username = $api.getStorage('username');
		   var headimg = $api.getStorage('headimg');
		    if(!userid){
		        api.toast({
	                      msg:'未登录'
                   });
            var setCss = 'setCss(4);';
				api.execScript({
						name: 'root',
						script: setCss
				}); 
                return false;
			}else{
			 //创建美洽
             var mq = api.require('meiQia');
             //设置用户信息
             var infoParam = {
                 email: userid+"@leduobao.com",
                 name: username,
                 avatar: headimg,
                 comment:api.systemType+api.systemVersion,
                 tags: [api.systemType]
                 };
                 mq.setClientInfo(infoParam);
             //设置自定义用户Id
             var customizedIdParam = {
                 id:userid,
                 };
                 mq.setLoginCustomizedId(customizedIdParam); 
                 mq.show();
			}
		}		